package net.dgg.iboss.sc.backservice.task.service;

import net.dgg.iboss.sc.backservice.task.dao.UserRecorderDAO;
import net.dgg.iboss.sc.backservice.task.util.StringUtils;
import net.dgg.iboss.sc.backservice.task.entity.UserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户管理Service
 *
 * @Author hdc
 * @Date 2018/4/11 13:42
 */
@Service("usersService")
public class UserService {

    @Autowired
    UserRecorderDAO userRecorderDAO;
    @Autowired
    OrganizationManager organizationManager;


    /**
     * 根据角色code查询指定角色用户
     *
     * @param codes  角色code ,分割
     * @param status 用户在职状态
     * @return
     */
    public List<UserEntity> findUserByRoleCodes(String[] codes, Integer status) {
        return userRecorderDAO.findUserByRoleCodes(codes, status);
    }

    /**
     * 通过用户ID查询用户实体
     *
     * @param userId 用户Id
     * @return
     */
    public UserEntity findUserById(long userId) {
        return userRecorderDAO.findUserEntityByUserId(userId);
    }

    /**
     * 通过用户工号查询用户实体
     *
     * @param loginName 用户工号
     * @return
     */
    public UserEntity findUserByLoginName(String loginName) {
        return userRecorderDAO.findUserEntityByLoginName(loginName);
    }

    /**
     * 查询指定部门下的子部门中包含指定角色的指定状态人员
     *
     * @param codes
     * @param status
     * @param orgId
     * @return
     */
    public List<UserEntity> findUserByRoleCodesAndOrgs(String[] codes, Integer status, Long orgId) {
        List<Long> orgIds = new ArrayList<>();
        orgIds = organizationManager.findOrgIdsByPId(orgId);
        orgIds.add(orgId);
        return userRecorderDAO.findUserByRoleCodesAndOrgs(codes, 0, orgIds);
    }
}
